# Create your views here.
from l2ldjango.apps.blog.views import *
from l2ldjango.apps.blog.models import *
from l2ldjango.apps.blog.forms import *
from django.template import RequestContext 
from django.shortcuts import render_to_response, get_object_or_404
from l2ldjango.settings import DATABASES
from django.contrib.auth.decorators import login_required
from l2ldjango.apps.category.models import *

def get_blogdetails(request,slugname):
    blogs=Blog.objects.get(slug=slugname)
    form=CommentForm()
    variables = RequestContext(request,{'blogs':blogs,'form':form})    
    return render_to_response('blog/blog-single.html',variables)

def runquery(qry):
    import MySQLdb

    # Open database connection
    db = MySQLdb.connect("localhost",DATABASES['default']['USER'],DATABASES['default']['PASSWORD'],DATABASES['default']['NAME'] )
    
    # prepare a cursor object using cursor() method
    cursor = db.cursor()
    
    # execute SQL query using execute() method.
    cursor.execute(qry)
    
    # Fetch a single row using fetchone() method.
    #data = cursor.fetchone()
    
    #print "Database version : %s " % data
    
    # disconnect from server
    db.close()


def legalBlogByCategory(request):
    cat=request.GET['cat']
    if cat=='0':
        blogs=Blog.objects.filter(status=3,is_public=0,category__isnull=True).order_by("-creation_date")
    else:
        cat_model=Category.objects.get(pk=cat)
        blogs=Blog.objects.filter(status=3,is_public=0,category=cat_model).order_by("-creation_date")
    variables = RequestContext(request,{'blogs':blogs})
    return render_to_response('blog/legal-blog.html',variables)

def legalBlog(request):
    blogs=Blog.objects.filter(status=3,is_public=0).order_by("-creation_date")
    variables = RequestContext(request,{'blogs':blogs})
    return render_to_response('blog/legal-blog.html',variables)

@login_required
def view_blog(request,slugname):
    blogs=Blog.objects.get(slug=slugname)
    form=CommentForm()
    variables = RequestContext(request,{'blogs':blogs,'title':blogs.title,'form':form})    
    #return HttpResponse(blogs);
    return render_to_response('blog/blog-single.html',variables) 


@login_required   
def commentBlog(request):
    blogid=request.POST['blogid']
    blogs=Blog.objects.get(id=blogid)
    if request.method=='POST':
        a=BlogComments()
        #a.user_id=request.user
        form = CommentForm(request.POST)
        if form.is_valid():             
            f_comment=BlogComments()
            f_comment.comment=request.POST['comment']  
            f_comment.commented_by=request.user
            f_comment.save()                      
            qry='%s%s%s%s%s'%('insert into blog_blog_comments(blog_id,blogcomments_id) values (',blogid,',',f_comment.id,')')
            runquery(qry)
            #blogs.comments=lst
            #blogs.save()             
            formnew = CommentForm()
            variables= RequestContext(request,{'commentmessage':'successfully commented!!','blogs':blogs,'form':formnew})
            return render_to_response('blog/blog-single.html',variables)
            #return render_to_response('registration/success_page.html',variables)
        else:
            errs=form.errors
            form = CommentForm(request.POST)
            variables = RequestContext(request,{'form': form,'blogs':blogs,'message':errs})
            return render_to_response('blog/blog-single.html',variables)
            
    
